<?php

/**
 * This is the model class for table "library".
 *
 * The followings are the available columns in table 'library':
 * @property integer $identification
 * @property string $name
 * @property string $description
 * @property integer $width
 * @property integer $height
 * @property string $folder
 * @property integer $is_system
 * @property string $note
 * @property string $insert
 * @property string $update
 * @property string $delete
 *
 * The followings are the available model relations:
 * @property Items[] $items
 */
class Library extends CActiveRecord
{
    public $key;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Library the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'library';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, folder, insert', 'required'),
            array('width, height, is_system', 'numerical', 'integerOnly'=>true),
            array('name, folder', 'length', 'max'=>255),
            array('description, note, update, delete', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('identification, name, description, width, height, folder, is_system, note, insert, update, delete', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'items' => array(self::HAS_MANY, 'Items', 'library'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'identification' => 'Identification',
            'name' => 'Name',
            'description' => 'Description',
            'width' => 'Width',
            'height' => 'Height',
            'folder' => 'Folder',
            'is_system' => 'Is System',
            'note' => 'Note',
            'insert' => 'Insert',
            'update' => 'Update',
            'delete' => 'Delete',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('identification',$this->identification);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('description',$this->description,true);
        $criteria->compare('width',$this->width);
        $criteria->compare('height',$this->height);
        $criteria->compare('folder',$this->folder,true);
        $criteria->compare('is_system',$this->is_system);
        $criteria->compare('note',$this->note,true);
        $criteria->compare('insert',$this->insert,true);
        $criteria->compare('update',$this->update,true);
        $criteria->compare('delete',$this->delete,true);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }


    /** getDataByParameter =============================
     * @author   TuanNguyen
     * @desc     save image entity to database
     * @param    $i_page: page to get next data
     *           $i_amountPerPage: amount data per page to display
     *           $s_order: string is defined for order of field
     *           $s_where: condition to get data
     *           $s_where: reference parameter to set all amount of data
     * @return   boolean
     *           ErrorEntity: if there are an error in database
     * ============================================== */
    public static function getDataByParameter($i_page, $i_amountPerPage, $s_order, $s_where, &$i_total)
    {
        //prepare order statement
        $i_total = Yii::app()->db->createCommand("Select Count(*) from `library` where ".$s_where.";")->queryScalar();;
         
        $command = Yii::app()->db->createCommand("CALL library_01_getList(:p_page,:p_amount,:p_oder,:p_where)");
        $command->bindValue("p_page",$i_page);
        $command->bindValue("p_amount",$i_amountPerPage);
        $command->bindValue("p_oder",$s_order);
        $command->bindValue("p_where",$s_where);
         
        try
        {
            $arr_model = array();
            $arr_data = $command->queryAll();

            foreach ( $arr_data as $key => $row)
            {
                $m_model = new Library();
                $m_model->unsetAttributes();

                $m_model->key         = $row["identification"];
                $m_model->name        = $row["name"];
                $m_model->description = $row["description"];
                $m_model->width       = $row["width"];
                $m_model->height      = $row["height"];
                $m_model->folder      = $row["folder"];
                $m_model->is_system   = $row["is_system"];
                $m_model->note        = $row["note"];
                $m_model->insert      = $row["insert"];
                $m_model->update      = $row["update"];
                $m_model->delete      = $row["delete"];

                array_push($arr_model, $m_model);
            }//End foreach

            return $arr_model;
        }//End Try
        catch (CDbException $ex)
        {
            $o_error            = new ErrorEntity();
            $o_error->errCode   = "ERR_1";
            $o_error->param     = null;
            return $o_error;
        }//End catch
    }//End getDataByParameter


    /** getDataByID =============================
     * @author   TuanNguyen
     * @desc     get product entity from database
     * @param    integer $i_id: key of product record
     * @return   Product Entity
     *           ErrorEntity: if there are an error in database
     * ============================================== */
    public static function getDataByID($i_id)
    {
        //Set SQL command
        $command = Yii::app()->db->createCommand("CALL library_02_getid(:p_id)");

        //Set parameter for SQL command
        $command->bindValue("p_id", $i_id,PDO::PARAM_INT);

        try
        {
            $row = $command->queryRow();

            if($row !=null)
            {
                $m_model = new Library();
                $m_model->unsetAttributes();

                $m_model->key         = $row["identification"];
                $m_model->name        = $row["name"];
                $m_model->description = $row["description"];
                $m_model->width       = $row["width"];
                $m_model->height      = $row["height"];
                $m_model->folder      = $row["folder"];
                $m_model->is_system   = $row["is_system"];
                $m_model->note        = $row["note"];
                $m_model->insert      = $row["insert"];
                $m_model->update      = $row["update"];
                $m_model->delete      = $row["delete"];

                return $m_model;
            }

            return null;
        }
        catch (CDbException $ex)
        {
            $o_error            = new ErrorEntity();
            $o_error->errCode   = "ERR_2";
            $o_error->param     = array("SQLException" => $ex->getMessage());
            return $o_error;
        }
    }//End getDataByID
}